<?php
namespace App\Http\Controllers\Front;


use App\Models\UserPerformance;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Redis;
use App\Models\Keys;
use Curl\Curl;
use App\Models\CorpInfo;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\Models\RoleUser;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use App\Models\Company;
use App\Models\Register;
use App\Models\RegUser;
use App\Models\Code;
use App\Models\Order;


class CimoAdminController extends Controller
{

    public function __construct()
    {
        if(empty(Auth::user())){
            return redirect('/front/admin');
        }
        if(Auth::user()->type != 3){
            abort(403,'您没有权限查看');
        }
    }
    
    
    //管理界面首页
    public function getIndex()
    {
        $view = 'cimo_admin.index';
        return view($view);
    }

    //企业列表
    public function getCorplist(Request $request)
    {
        $name = $request->input('name','');
        $query = Company::where('init_index','2');
        if(!empty($name)){
            $query->where('name','like','%'.$name.'%');
        }
        $coms = $query->paginate(20);
        $view = 'cimo_admin.corplist';
        return view($view)
            ->with('name',$name)
            ->with('coms',$coms);
    }


    //成员列表
    public function getMemberlist(Request $request)
    {
        $comid = $request->input('comid',0);
        if($comid <=0){
            abort(404);
        }
        $company = Company::find($comid);
        if(empty($company)){
            abort(404);
        }
        $member =  User::where('corpid',$company->corpid)->where('status','1')->paginate(20);
        foreach ($member as $m){
            $m->roles = RoleUser::where('corpid',$m->corpid)->where('user_id',$m->id)->select('role_id')->get();
        }
        $rolearray = array('9'=>'系统管理员','10'=>'考勤管理员','11'=>'业绩管理员','12'=>'奖惩管理员');
        $view = 'cimo_admin.memberlist';
        return view($view)
            ->with('rolearray',$rolearray)
            ->with('company',$company)
            ->with('member',$member);
    }


    //封停或解封企业
    public function postChangeStatus(Request $request)
    {
        $result = array(
            'status'        => 'success',
            'msg'           => 'ok',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $status = $request->input('status',1);
        if($status != 1 && $status !=2){
            $result['status'] = 'fail';
            $result['msg'] = '状态无效';
            return Response::json($result);
        }
        $id = $request->input('id',0);
        if($id <= 0){
            $result['status'] = 'fail';
            $result['msg'] = 'id无效';
            return Response::json($result);
        }
        $company = Company::find($id);
        if(empty($company)){
            $result['status'] = 'fail';
            $result['msg'] = '公司信息不存在';
            return Response::json($result);
        }
        $company->status = $status;
        $company->save();
        if($status == 1){
            $result['data'] = '1';
        }else{
            $result['data'] = '2';
        }

        return Response::json($result);
    }


    //订单列表
    public function getOrderlist(Request $request)
    {
        $name = $request->input('name','');
        //$coms = Company::where('init_index','2')->where('name','like','%'.$name.'%');
        $orders = Order::paginate(20);
       
        $view = 'cimo_admin.orderlist';
        return view($view)
            ->with('name',$name)
            ->with('orders',$orders);
    }

    

  
}

